package com.codedancer.campus.client.api.confessionalWallComment;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.confessionalWallComment.ro.CommentAddRo;
import com.codedancer.campus.client.api.confessionalWallComment.ro.ConfessionalWallCommentPageSearchRo;
import com.codedancer.campus.client.service.IConfessionalWallCommentService;
import com.codedancer.campus.client.service.bo.confessionalWallComment.CommentAddBo;
import com.codedancer.campus.client.service.bo.confessionalWallComment.ConfessionalWallCommentPageSearchBo;
import com.codedancer.campus.client.service.vo.confessionalWallComment.ConfessionalWallCommentVo;
import com.codedancer.campus.common.anno.CommentNumber;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.enums.CommentNumberKeyEnum;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/confessional-wall-comment")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "表白墙评论Api", description = "表白墙评论Api", tags = "表白墙评论Api")
public class ConfessionalWallCommentApi extends BaseApi {

  @Autowired
  private IConfessionalWallCommentService confessionalWallCommentService;

  @PostMapping("/add/{confessionalWallUuid}")
  @ApiOperation("表白墙评论")
  @CommentNumber(description = "表白墙评论", key = CommentNumberKeyEnum.CONFESSIONAL_WALL_COMMENT)
  public ApiResponse<Void> add(
      @PathVariable(value = "confessionalWallUuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String confessionalWallUuid,
      @RequestBody @Valid @ApiParam(value = "表白墙评论添加Ro", required = true)
      CommentAddRo commentAddRo) {
    CommentAddBo commentAddBo = BeanUtil.copyProperties(commentAddRo, CommentAddBo.class);
    commentAddBo.setCurrentUserUuid(currentLoginUuid());
    commentAddBo.setConfessionalWallUuid(confessionalWallUuid);
    confessionalWallCommentService.add(commentAddBo);
    return success();
  }

  @GetMapping("/page/parent-comment/{confessionalWallUuid}")
  @ApiOperation("分页查询表白墙父级评论")
  public ApiResponse<IPage<ConfessionalWallCommentVo>> pageParentComment(
      @PathVariable(value = "confessionalWallUuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String confessionalWallUuid,
      @ApiParam(value = "表白墙分页查询Ro", required = false) ConfessionalWallCommentPageSearchRo confessionalWallCommentPageSearchRo) {
    ConfessionalWallCommentPageSearchBo confessionalWallCommentPageSearchBo = BeanUtil
        .copyProperties(confessionalWallCommentPageSearchRo,
            ConfessionalWallCommentPageSearchBo.class);
    confessionalWallCommentPageSearchBo.setConfessionalWallUuid(confessionalWallUuid);
    return success(
        confessionalWallCommentService.pageParentComment(confessionalWallCommentPageSearchBo));
  }

  @PostMapping("/reply/{confessionalWallCommentUuid}")
  @ApiOperation("表白墙回复")
  public ApiResponse<Void> reply(
      @PathVariable(value = "confessionalWallCommentUuid", required = true) @ApiParam(value = "评论uuid", required = true) String confessionalWallCommentUuid,
      @RequestBody @Valid @ApiParam(value = "表白墙评论添加Ro", required = true)
      CommentAddRo commentAddRo) {
    CommentAddBo commentAddBo = BeanUtil.copyProperties(commentAddRo, CommentAddBo.class);
    commentAddBo.setCurrentUserUuid(currentLoginUuid());
    commentAddBo.setConfessionalWallCommentUuid(confessionalWallCommentUuid);
    confessionalWallCommentService.reply(commentAddBo);
    return success();
  }

  @GetMapping("/page/children-comment/{confessionalWallCommentUuid}")
  @ApiOperation("分页查询该评论下子集评论")
  public ApiResponse<IPage<ConfessionalWallCommentVo>> pageChildrenComment(
      @PathVariable(value = "confessionalWallCommentUuid", required = true) @ApiParam(value = "表白墙评论顶级uuid", required = true) String confessionalWallCommentUuid,
      @ApiParam(value = "表白墙分页查询Ro", required = false) ConfessionalWallCommentPageSearchRo confessionalWallCommentPageSearchRo) {
    ConfessionalWallCommentPageSearchBo confessionalWallCommentPageSearchBo = BeanUtil.copyProperties(
        confessionalWallCommentPageSearchRo, ConfessionalWallCommentPageSearchBo.class);
    confessionalWallCommentPageSearchBo.setConfessionalWallCommentUuid(confessionalWallCommentUuid);
    return success(
        confessionalWallCommentService.pageChildrenComment(confessionalWallCommentPageSearchBo));
  }

  @DeleteMapping("/delete/{confessionalWallCommentUuid}")
  @ApiOperation("删除评论")
  public ApiResponse<Void> delete(
      @PathVariable(value = "confessionalWallCommentUuid", required = true) @ApiParam(value = "表白墙评论uuid", required = true) String confessionalWallCommentUuid) {
    confessionalWallCommentService.delete(confessionalWallCommentUuid);
    return success();
  }
}
